package com.jtjrenren.android.taxi.passenger.base;

import android.app.Service;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.R;
import com.library.weidget.CustomeWaittingDialog;

import butterknife.ButterKnife;


/**
 * Created by Administrator on 2015/12/21.
 */
public abstract class BaseActivity extends AppCompatActivity {
    public static final int CODE_SHOW = 300;

    protected Handler handler;
    protected CustomeWaittingDialog waittingDialog;
    protected SoundPool soundPool;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        AppManger.getInstance().currentActivity = this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (waittingDialog != null)
            waittingDialog = null;

        if (soundPool != null)
            soundPool.release();
    }


    protected void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case CODE_SHOW:
                        displayShort(msg.obj.toString());
                        break;
                    default:
                        handMsg(msg);
                        break;
                }
            }
        };
    }


    public abstract int getLayoutId();

    public abstract void init();

    protected void handMsg(Message msg) {
    }


    protected void displayShort(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    protected void showWaittingDialog(String title, boolean outCancle) {
        if (waittingDialog == null)
            waittingDialog = new CustomeWaittingDialog(this, title);
        else
            waittingDialog.setDesc(title);
        waittingDialog.setCanceledOnTouchOutside(outCancle);
        waittingDialog.show();
    }

    protected void dismissWaittingDialog() {
        if (waittingDialog != null && waittingDialog.isShowing())
            waittingDialog.dismiss();
    }


    protected void playDingDong() {
        initSoundPool();
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                //int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) ：
                // 该方法的第一个参数指定播放哪个声音； leftVolume 、 rightVolume 指定左、右的音量：
                // priority 指定播放声音的优先级，数值越大，优先级越高；
                // loop 指定是否循环， 0 为不循环， -1 为循环； rate 指定播放的比率，数值可从 0.5 到 2 ， 1 为正常比率。
                soundPool.play(sampleId, 1, 1, 0, 0, 1);
            }
        });
    }

    private int initSoundPool() {
        /**
         * 21版本后，SoundPool的创建发生很大改变
         */
        //判断系统sdk版本，如果版本超过21，调用第一种
        if (Build.VERSION.SDK_INT >= 21) {
            SoundPool.Builder builder = new SoundPool.Builder();
            builder.setMaxStreams(2);//传入音频数量
            //AudioAttributes是一个封装音频各种属性的方法
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);//设置音频流的合适的属性
            builder.setAudioAttributes(attrBuilder.build());//加载一个AudioAttributes
            soundPool = builder.build();
        } else {
            soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        }
        //load的返回值是一个int类的值：音频的id，在SoundPool的play()方法中加入这个id就能播放这个音频
        return soundPool.load(getApplicationContext(), R.raw.order_success, 1);
    }

    /**
     * 震动
     */
    protected void vibratorF() {
        Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
        vibrator.vibrate(500);
    }

}
